#!/usr/bin/perl -w

#bidirectional open
use IPC::Open2;

#threading support
use threads;
use Thread::Queue;

#create my filehandle storage
my $gphotoin;
my $gphotoout;

# open the process
$pid = open2( $gphotoout , $gphotoin , 'sudo gphoto2 --force-overwrite --shell');
print "Opened as $pid\n";


#define some queues for it.
my $inputqueue = Thread::Queue->new();
my $outputqueue = Thread::Queue->new();

#spin up an asynchronous thread for reading from the shell
async{
	$outputqueue->enqueue( $_ ) while <$gphotoout>;
	$outputqueue->enqueue( undef );
}->detach();



#prepare capture commands

#ramp up tval
foreach $tvalindex ( reverse(4..49) ) {
	
	@commands = ( "set-config-index shutterspeed=$tvalindex\n" );
	$inputqueue->enqueue( @commands );
	
}


#take shots
for (1..60) {
	
	$inputqueue->enqueue( undef );
	
}

#ramp down tval
foreach $tvalindex (4..52) {
	
	@commands = ( "set-config-index shutterspeed=$tvalindex\n" );
	$inputqueue->enqueue( @commands );
	
}

@commands = ( "exit" );
$inputqueue->enqueue( @commands );



#begin capture
print $gphotoin "capture-image-and-download\n";
print $gphotoin "wait-event-and-download 1\n";
my $iswaiting = time();




while (1) {
	#now set up a loop for checking from the pipe
	while ( $outputqueue->pending() ) {
		
		#get the message from gphoto
		my $line = $outputqueue->dequeue() or last;
		
		#camera says ready for capture
		
		
		
		#saving an image
		if ( $line =~ /Saving/ ) {
			
			print "Saving to pi.\n";
			
			
			#add next capture here.
			print "Camera ready for capture.\n";
			
			@commands = $inputqueue->dequeue();
			
			if ( $commands[0] ) {
				
				if ($command[0] eq "exit") {
					
					exit();
					
				}
				
				foreach $commandline (@commands) {
					
					print "sending: $commandline";
					print $gphotoin $commandline;
					
				}
				
			}
			
			print $gphotoin "capture-image-and-download\n";
			$iswaiting = time();
			
			
			
			
			
			
			
			next;
			
		}
		
		
		#deleting capture
		if ( $line =~ /Deleting/ ) {
			
			$iswaiting = time();
			next;
			
		}
		
		#finished capture
		if ( $line =~ /UNKNOWN Camera Status 0/ ) {
			
			$iswaiting = time();
			next;
			
		}
		
		if ( $line =~ /UNKNOWN/ ) {
			
			#ignore the unknown ptp messages
			#print "Unknown\n";
			print $line;
			$iswaiting = time();
			next;
			
		}
		
		if ( $line =~ /gphoto2/ ) {
			
			#it's the echo from the command line!
			next;
			
		}
		
		if ( $line =~ /Waiting/ ) {
			
			#response to command
			next;
			
		}
		
		if ( $line =~ /wait-event-and-download 1/ ) {
			
			#ignore command echo
			next;
			
		}
		
		if ( $line =~ /set-config-index/ ) {
			
			#ignore config index
			next;
			
		}
		
		print $line;
		
		
	}
	
	
	#if all else fails, sit and wait, refreshing the poll every second.
	if ( ( $iswaiting + 1 ) < time() ) {
		
		print "Timeout, polling camera.\n";
		print $gphotoin "wait-event-and-download 1\n";
		$iswaiting = time();
		
	}
	
	
}



#wait on the process to exit
#waitpid($pid,0);


	









